local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local TweenHelper = CommonOHOH.load.TweenHelper();
local CreateSkibi=CommonOHOH.config.Game.CreateSkibi
local PooLandTime=CreateSkibi.pooLandTime
local pooMove = TweenInfo.new(PooLandTime, Enum.EasingStyle.Linear,Enum.EasingDirection.Out);
local SkibiInitDt=CreateSkibi.skibiInitDt
local PooDt=CreateSkibi.PooDt
local MonsterClass=CommonOHOH.load.Monster()
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.LvId=self.parent:GetLevelId()
    self.Battle=self.parent.Battle
    self.Animatior=self.parent:GetPrivateComponent("animation")
    self.AttCtrl=self.Battle:GetPrivateComponent("AttCtrl")
    self.NeedHatch={}
end;

function M.dtor(self)
    --assert(self.dtorEd==nil," self.dtorEd=true--->")
    if  self.UpdateCon then
        self.UpdateCon:Disconnect()
        self.UpdateCon=nil
    end
    self.dtorEd=true
   -- print("self.dtorEd---A-->",self.dtorEd)
    self.NeedHatch={}
	M.super.dtor(self);
end

-- 播放吐便便动画
function M.PlayPoo(self)
    self.Animatior:PlayPoo()
end


function M.Born(self,tagCf,EffectParent,cid)
    --print("self.dtorEd---B-->",self.dtorEd)
    local bornEffectId="Effect.SkibiBorn_"..MonsterClass.GetSizeIdByCid(cid)
    local bornEffct=_G.POOL:GetObj(bornEffectId)
    require(bornEffct.PlayCtrl).Play(tagCf.Position,function()
        _G.POOL:PushObj(bornEffectId,bornEffct)
            if self.dtorEd then
                return 
            end
        local mobj=MonsterClass.new(self.LvId,cid,self.Battle,tagCf)
        mobj:Action()
    end,EffectParent)
end

function M.Update(self)

    if self.UpdateCon then
        return
    end

    self.UpdateCon=  self:AddProcess(function()
        while true do

            local count=#self.NeedHatch

            if count<=0 then
                if  self.UpdateCon then
                    self.UpdateCon:Disconnect()
                    self.UpdateCon=nil
                end
     
                return true
            end

            local m=self.NeedHatch[1]
            table.remove(self.NeedHatch,1)

            local tagCf=self.AttCtrl:CreateMonsterBornCframe()
            local EffectParent=self.AttCtrl:FindEffectFolder()
            local poo=_G.POOL:GetObj("Poo.1")
            poo.Position=self.parent:GetMousePos()
            poo.Parent=workspace
            self:PlayPoo()
            TweenHelper:PlayTween(poo, pooMove, {
                Position=tagCf.Position
            },function()
                _G.POOL:PushObj("Poo.1",poo)
                if self.dtorEd then
                    return 
                end
                -- self:Born(tagCf,EffectParent,m)
                self:Born(tagCf,nil,m)
            end)
            self:Wait(PooDt)
        end
    end)
end

function M.Hatch(self,list)
    for _,monster in pairs(list) do
       table.insert(self.NeedHatch,monster)
    end
    self:Update()
end
return M